<template>
	<view class="all">
		<view class="carp">
			<image class="logo1" src="@/static/img/logos.png" mode=""></image>
			<text class="carp_1">欢迎登录</text>
			<text class="carp_2">Welcome back</text>
			<view style="margin-top: 50rpx;">
				<view class="imput">
					<image class='logo2' src="@/static/img/sjp.png" />
					<view style="width: 90%;">
						<u-input type="text" v-model="from.mobile" placeholder="请输入您的手机号" />
					</view>
				</view>

				<view class="imput">
					<image class='logo3' style="width: 38rpx;" src="@/static/img/mima1.png" />
					<view style="width: 90%;">
						<u-input type="password" v-model="from.password" placeholder="请输入密码" />
					</view>
				</view>
				<text class="wangji">
					<text @click="submit('忘记密码')">忘记密码</text>
				</text>
			</view>
			<text class="denglu" @click="submit('登录')">登录</text>
			<view class="my">
				<text>没有账号？</text>
				<text @click="submit('注册')">注册</text>
			</view>
			<view class="qita">
				<u-divider>其它登录方式</u-divider>
				<view class="qita_1">
					<image @click="submit('微信')" src="@/static/img/wx.png" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				from: {
					mobile: "",
					password: "",
				}
			};
		},
		methods: {
			getUrlCode(name) { //截取code
				let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				let r = window.location.search.substr(1).match(reg);
				if (r != null) {
					return unescape(r[2]);
				}
				return null;
			},
			H5wxlogin() { //h5微信登录
			// window.location.href =
			// 	`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`
							
				let appid = 'wxf881d0e41dc44e79'
				const code = this.getUrlCode("code")
				const local = window.location.href + '&wx=1'
				if (code == null || code == "") {
				window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
					"&redirect_uri=" + encodeURIComponent(local) + "&response_type=code" +
					"&scope=snsapi_userinfo&state=1#wechat_redirect";
				} else {
					this.$api('user/WeChat', code).then(res => {
						if (res.code == 0) {
							this.$toast(res.msg, 0)
						} else {
							uni.setStorage({
								key: 'token',
								data: res.data.token
							})
							console.log(uni.getStorageSync('token'))
							this.$toast(res.msg, 1)
						}
					})
				}
			},

			submit(a) {
				if (a == '登录') {
					if (!this.from.mobile) {
						this.$toast('请输入手机号', 0)
						return
					}
					if (!this.$tel(this.from.mobile)) {
						this.$toast('手机号错误', 0)
						return
					}
					if (!this.from.password) {
						this.$toast('请输入密码', 0)
						return
					}
					this.$api('user/login', this.from).then(res => {
						if (res.code == 0) {
							this.$toast(res.msg, 0)
						} else {
							this.$toast(res.msg, 1)
							uni.setStorage({
								key: 'token',
								data: res.data.token
							})
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1000)
							console.log(uni.getStorageSync('token'))
						}
					})
				} else if (a == '忘记密码') {
					uni.navigateTo({
						url: './wangji'
					})
				} else if (a == '注册') {
					uni.navigateTo({
						url: './zhuce'
					})
				} else if (a == '微信') {
					this.H5wxlogin()
				}
			}
		}
	}
</script>

<style lang="scss">
	.carp {
		width: 75%;
		margin: auto;

		.qita {
			margin-top: 150rpx;

			.qita_1 {
				text-align: center;
				margin-top: 40rpx;

				image {
					width: 60rpx;
					height: 50rpx;
				}
			}
		}

		.my {
			text-align: center;
			padding-top: 20rpx;

			text:nth-of-type(1) {
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #131314;
				opacity: 1;
			}

			text:nth-of-type(2) {
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #FFD500;
				opacity: 1;

			}
		}

		.denglu {
			margin-top: 100rpx;
			display: block;
			width: 562rpx;
			height: 88rpx;
			background: #131314;
			opacity: 1;
			border-radius: 4rpx;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			text-align: center;
			line-height: 88rpx;
			font-weight: 400;
			color: #FFFFFF;
			opacity: 1;
		}

		.wangji {
			text-align: right;
			display: block;
			padding-top: 20rpx;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #91969A;
			opacity: 1;
		}

		.imput {
			border-bottom: 1rpx solid #E3E3E3;
			padding: 40rpx 0 10rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;

			image {
				width: 30rpx;
				height: 44rpx;
			}
		}

		.carp_2 {
			font-size: 30rpx;
			display: inline-block;
			margin-top: 10rpx;
			font-family: HarmonyOS Sans;
			font-weight: 400;
			color: #91969A;
			opacity: 1;
		}

		.carp_1 {
			margin-top: 40rpx;
			display: block;
			font-size: 36rpx;
			font-family: Source Han Sans CN;
			font-weight: bold;
			color: #131314;
			opacity: 1;

		}

		.logo1 {
			width: 220rpx;
			margin-top: 200rpx;
			height: 70rpx;
		}
	}
</style>
